package com.ican.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ican.domain.user.Resources;
import com.ican.service.user.ResourcesService;
import com.ican.util.ContextUtils;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class LoginInterceptor extends MethodFilterInterceptor {
    private static final long serialVersionUID = 1L;
    private static final Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);

    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        ActionContext ctx = invocation.getInvocationContext();
        addCategory(ctx);
        ctx.getSession().put(SystemConstants.KEY_ERROR_MSG, null);
        ctx.getSession().put(SystemConstants.KEY_SUCCESS_MSG, null);
        Object obj = ctx.getSession().get(SystemConstants.KEY_USER_SESSION);
        if (obj != null) {
            try{
                return invocation.invoke();
            }catch(BusinessException e){
                ctx.getSession().put(SystemConstants.KEY_ERROR_MSG, e.getMessage());
                logger.error(e.getMessage(),e);
                return Action.ERROR;
            }catch(Exception e){
                ctx.getSession().put(SystemConstants.KEY_ERROR_MSG, SystemConstants.MSG_ERROR_DEFAULT);
                logger.error(e.getMessage(),e);
                return Action.ERROR;
            }
        }
        return Action.LOGIN;
    }
    
    private void addCategory(ActionContext ctx){
        //取页面传过来的导航类目编码
        Object obj_request = ctx.getParameters().get(SystemConstants.KEY_CATEGORY_ID);
        if(obj_request != null){
            String[] requestCategory = (String[])obj_request;
            //取保存在session的导航类目编码
            Object obj_session = ctx.getSession().get(SystemConstants.KEY_CATEGORY_ID);
            if(obj_session != null){
                String sessionCategory = obj_session.toString();
                if(sessionCategory.equals(requestCategory[0])){
                    return;
                }else{
                    addCategory2(requestCategory[0], ctx);
                }
            }else{
                addCategory2(requestCategory[0], ctx);
            }
        }
    }
    
    private void addCategory2(String requestCategory, ActionContext ctx){
        ResourcesService resourcesService = (ResourcesService) ContextUtils.getBeanFromSpringContext("resourcesService");
        Resources rs = resourcesService.findResourceById(Integer.valueOf(requestCategory));
        if(rs != null){
            ctx.getSession().put(SystemConstants.KEY_TITLE_DESC, rs.getName());
            String cn = rs.getName();
            while(rs.getPid() != null && rs.getPid() != 1){
                rs = resourcesService.findResourceById(rs.getPid());
                if(rs == null){
                    break;
                }
                cn = rs.getName() + ">" + cn;
            }
            ctx.getSession().put(SystemConstants.KEY_CATEGORY_ID, requestCategory);
            ctx.getSession().put(SystemConstants.KEY_NAVIGATION_DESC, cn);
        }
    }
}
